﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using System.Reflection;
using System.Data;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQuerySortELBDao : DaoService<DataQuerySort>
    {
        public override string TableName => "fas_dataquerysorting";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<DataQuerySort>();

        public DataQuerySortELBDao() : base()
        {
            DBName = "TFAS";
        }

        public override Dictionary<string, object> GetDataMapping(DataQuerySort data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("SortColumnType");
            datas.Add("SortColumnType", (int)data.SortColumnType);
            return datas;
        }

        public override DataQuerySort ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("SortColumnType"))
                columns.Remove("SortColumnType"); 
            var data = base.ConvertToObject(columns, row);

            var columnTypeStr = Convert.ToString(row["SortColumnType"]);
            if (string.IsNullOrEmpty(columnTypeStr))
            {
                data.SortColumnType = ColumnType.Normal;
            }
            else
            {
                data.SortColumnType = (ColumnType)Enum.Parse(typeof(ColumnType), columnTypeStr);
            }

            return data;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="dqId"></param>
        /// <returns></returns>
        public List<DataQuerySort> GetDataQuerySorts(string dqId)
        {
            var result = new List<DataQuerySort>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{dqId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataQuerySort));
            }
            return result;
        }

        /// <summary>
        /// 添加DataQuerySort
        /// </summary>
        /// <param name="dqSorts"></param>
        public void CreateDataQuerySort(List<DataQuerySort> dqSorts)
        {
            try
            {
                base.Add(dqSorts);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateSortError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySort
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQuerySort(string dqId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSortError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySource
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQuerySort(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSortError, e.Message, e);
            }
        }
    }
}
